<?php

namespace app\admin\controller;

use app\model\Agent;
use app\model\Incomelog;
use app\model\Member;
use app\model\Order;
use think\exception\ValidateException;
use app\model\AgentLevel;

class AgentController extends Base
{

	function index()
	{
	    $model = new Member();
		$weid = weid();
		$keyword = input('post.keyword', '', 'serach_in');
		$type    = input('type',1);
        $query = $model->where(['weid' => $weid]);
        $pid   = input('pid');
		if($type == 1){
            $query = $query->where(['pid' => $pid]);
        }else{
		    $list  = $model->where(['pid'=>$pid])->field('id')->select()->toArray();
		    $pids  = '';
		    foreach ($list as $k=>$v){
                $pids .=','.$v['id'];
            }
            $query = $query->whereIn('pid', $pids);
        }

		if (!empty($keyword)) {
			$query->where('nickname', 'like', '%' . $keyword . '%');
		}

		$datalist = $query->order('id asc')->paginate()->toArray();
        if($datalist){
            foreach ($datalist['data'] as &$vo) {
                $vo['num']       = $model->where(['pid'=>$vo['id']])->count();
                $vo['order_num'] = Order::where(['uid'=>$vo['id']])->count();
                $vo['create_time'] = time_format($vo['create_time']);
                $vo['update_time'] = time_format($vo['update_time']);
            }
        }


		$data['data'] = $datalist;
		return $this->json($data);
	}

    public function teamorder()
    {

        $weid = weid();
        $status = input('post.status');
        $ptype = input('post.ptype');
        $uid   = input('post.uid');
        $where['weid'] = $weid;
        $where['uid']  = $uid;

        $orderids = Incomelog::where($where)->column('order_id');

        $query = Order::where(['weid' => $weid]);

        if (!empty($ptype)) {
            $query->where('ptype', $ptype);
        }

        if (!empty($status)) {
            $query->where('order_status_id', $status);
        }

        $query->where('id', 'in', $orderids);
        $res = $query->order('id desc')
            ->paginate(getpage())
            ->toArray();

        $res['data'] = Order::setOrderList($res['data']);
        foreach ($res['data'] as $k=>&$v){
            $v['Incomelog'] = Incomelog::where(['order_id'=>$v['id']])->find();
//            $v['username']  = Member::where(['id'=>$v['uid']])->find();
        }
        $data['data'] = $res;

        return $this->json($data);
    }
}
